package acwing;

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class ac1562 {
    static final int N = 1010, M = 100010;
    static int n, L, k, idx;
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] ne = new int[M];
    static boolean[] st = new boolean[N];
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx ++ ;
    }

    static int bfs(int x) {
        Queue<Integer> q = new LinkedList<>();
        Arrays.fill(st, false);
        q.offer(x);
        st[x] = true;
        int res = 0;

        for (int i = 0; i < L; i ++ ) {
            int sz = q.size();
            while (sz -- > 0) {
                int t = q.poll();
                for (int j = h[t]; j != -1; j = ne[j]) {
//                    int x = e[j];
                    int num = e[j];
                    if (!st[num]) {
                        q.offer(num);
                        res ++ ;
                        st[num] = true;
                    }
                }
            }
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
//        n = sc.nextInt();
//        L = sc.nextInt();
        String[] op = in.readLine().split(" ");
        n = Integer.parseInt(op[0]);
        L = Integer.parseInt(op[1]);
//        System.out.println(n + " " + L);
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i ++ ) {
            String[] op2 = in.readLine().split(" ");
            int x = Integer.parseInt(op2[0]);
            for (int j = 1; j <= x; j ++ ) {
                int num = Integer.parseInt(op2[j]);
                add(num, i);
            }
        }
//        for (int i = 1; i <= n; i ++ ) {
//            int x = sc.nextInt();
//            while (x -- > 0) {
//                int num = sc.nextInt();
//                add(num, i);
//            }
//        }
        String[] op3 = in.readLine().split(" ");
        k = Integer.parseInt(op3[0]);
        for (int i = 1; i <= k; i ++ ){
            int num = Integer.parseInt(op3[i]);
            int res = bfs(num);
            out.println(res);
        }
//        k = sc.nextInt();
//        while (k -- > 0) {
//            int num = sc.nextInt();
//            int res = bfs(num);
//            out.println(res);
//        }
        out.close();
    }
}
